package talk.dapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;

import talk.Bean.MyUser;

import com.talk.debby.talk.R;

import talk.listener.OnItemClick;

import java.util.List;

/**
 * Created by Administrator on 2018/4/8.
 * 好友列表
 */

public class AdapterUserList extends RecyclerView.Adapter<AdapterUserList.ViewHolder> {

    private List<MyUser.FlistBean> users;
    private OnItemClick click;
    private Context context;

    public AdapterUserList(Context context, List<MyUser.FlistBean> users, OnItemClick click) {
        this.context = context;
        this.users = users;
        this.click = click;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_user_list, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(R.mipmap.ic_hippo)
                .placeholder(R.mipmap.ic_loading)
                .priority(Priority.HIGH);
        Glide.with(context)
                .load(users.get(position).getImgurl())
                .apply(options)
                .into(holder.portrait);
        holder.userId.setText(users.get(position).getUname());
        holder.userId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click.onClick(users.get(position).getUid(), users.get(position).getUname());
            }
        });
    }

    @Override
    public int getItemCount() {
        return users == null ? 0 : users.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView portrait;
        TextView userId;

        public ViewHolder(View view) {
            super(view);
            portrait = view.findViewById(R.id.iv_portrait);
            userId = view.findViewById(R.id.tv_user_id);
        }
    }


}
